package com.test.demo.ext

import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.test.demo.R

import com.test.demo.utils.glide.base.LoaderBuilder
import com.test.demo.utils.glide.widget.SimpleDraweeView


/**
 * @Description: glide 扩展
 */
fun ImageView.loadCircle(url: Any?,borderWidth: Int = 0,borderColor:Int = R.color.trans) {
    if (url == null) {
        return
    }
}


fun ImageView.loadUrl(url: Any?,round:Int=0 ,borderWidth: Int = 0,borderColor:Int = R.color.trans) {
    if (url == null) {
        return
    }
}


fun ImageView.loadRound(url: Any?,round:Int=0 ,borderWidth: Int = 0,borderColor:Int = R.color.trans) {
    if (url == null) {
        return
    }
}

fun SimpleDraweeView.loadUrl(url: Any?) {
    if (url == null) {
        return
    }else{
        if (this == null) {
            return
        }
        //ic_avatar_default  ic_home_sel
        val defaultImage: Drawable? = context.getDrawable(com.example.common_library.R.mipmap.ic_avatar_default)
        val builder = LoaderBuilder().load(url)

//        builder.skipMemory = true
//        builder.diskCache = LoaderBuilder.DISK_CACHE_ALL

        builder.scaleType = this.scaleType
        if (this is SimpleDraweeView) {
            val placeHolderImage = this.placeHolderImage
            if (placeHolderImage != null) {
                builder.placeholder(placeHolderImage)
            } else {
                if (defaultImage != null) {
                    builder.placeholder(defaultImage)
                }
            }
            val roundAsCircle = this.isRoundAsCircle
            if (roundAsCircle) {
                builder.circle(true)
            } else {
                val roundedCornerRadius = this.roundedCornerRadius
                if (roundedCornerRadius > 0) {
                    if (!this.isRoundTopLeft && !this.isRoundTopRight && !this.isRoundBottomRight && !this.isRoundBottomLeft) {
                        // 兼容旧版
                        builder.round(
                            floatArrayOf(
                                roundedCornerRadius.toFloat(),
                                roundedCornerRadius.toFloat(),
                                roundedCornerRadius.toFloat(),
                                roundedCornerRadius.toFloat()
                            )
                        )
                    } else {
                        builder.round(
                            floatArrayOf(
                                if (this.isRoundTopLeft) roundedCornerRadius.toFloat() else 0.toFloat(),
                                if (this.isRoundTopRight) roundedCornerRadius.toFloat() else 0.toFloat(),
                                if (this.isRoundBottomRight) roundedCornerRadius.toFloat() else 0.toFloat(),
                                if (this.isRoundBottomLeft) roundedCornerRadius.toFloat() else 0.toFloat()
                            )
                        )
                    }
                } else {
                    // 新版本使用
                    builder.round(
                        floatArrayOf(
                            this.roundTopLeftRadius.toFloat(),
                            this.roundTopRightRadius.toFloat(),
                            this.roundBottomRightRadius.toFloat(),
                            this.roundBottomLeftRadius.toFloat()
                        )
                    )
                }
            }
            // 边框
            val borderWidth = this.borderWidth
            if (borderWidth > 0) {
                val borderColor = this.borderColor
                builder.borderWidth = borderWidth.toFloat()
                builder.borderColor = borderColor
            }
        } else {
            if (defaultImage != null) {
                builder.placeholder(defaultImage)
            }
        }
        // 动画
        builder.into(this)
    }
}

